package com.sosv.breweryDB.connector.service.style;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Inject;
import com.sosv.breweryDB.connector.entity.Style;
import com.sosv.breweryDB.connector.entity.StyleResultPage;
import com.sosv.breweryDB.connector.service.exceptions.ApiKeyNotFoundExeption;
import com.sosv.breweryDB.connector.service.resource.filter.beer.IBeersFilter;
import com.sosv.breweryDB.connector.service.resource.style.IStyleResource;

public class StyleService implements IStyleService {

	private IStyleResource styleResource;
	
	@Inject
	public StyleService(IStyleResource styleResource) {
		this.styleResource = styleResource;
	}
	
	@Override
	public List<Style> getAllStyles() throws ApiKeyNotFoundExeption {
		// A set would be faster but we take a list here because of sorting
		// functionality in future releases
		List<Style> styles = new ArrayList<Style>();
		StyleResultPage firstPage = this.styleResource.getStyles(null, null);

		styles.addAll(handlePage(firstPage, null));

		return styles;

	}
	
	private List<? extends Style> handlePage(StyleResultPage page,
			IBeersFilter filter) throws ApiKeyNotFoundExeption {
		List<Style> styles = page.getData();
		if (styles == null || styles.isEmpty()) {
			styles = new ArrayList<Style>();
		}
		
		this.styleResource.getStyles(0, filter);
		

		return styles;
	}

	
}
